package cn.stylefeng.guns.modular.business.frequency.orderstock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderstock.entity.OrderStock;
import cn.stylefeng.guns.modular.business.frequency.orderstock.mapper.OrderStockMapper;
import cn.stylefeng.guns.modular.business.frequency.orderstock.model.param.OrderStockParam;
import cn.stylefeng.guns.modular.business.frequency.orderstock.service.OrderStockService;
import cn.stylefeng.guns.modular.business.frequency.orderstock.enums.OrderStockExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库(验车操作记录) 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderStockServiceImpl extends ServiceImpl<OrderStockMapper, OrderStock> implements OrderStockService {

    @Override
    public PageResult<OrderStock> page(OrderStockParam orderStockParam) {

        // 构造条件
        LambdaQueryWrapper<OrderStock> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderStock> list(OrderStockParam orderStockParam) {

        // 构造条件
        LambdaQueryWrapper<OrderStock> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderStockParam orderStockParam) {

        // 将dto转为实体
        OrderStock orderStock = new OrderStock();
        BeanUtil.copyProperties(orderStockParam, orderStock);

        this.save(orderStock);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderStockParam orderStockParam) {
        this.removeById(orderStockParam.getId());
    }

    @Override
    public void edit(OrderStockParam orderStockParam) {

        // 根据id查询实体
        OrderStock orderStock = this.queryOrderStock(orderStockParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderStockParam, orderStock);

        this.updateById(orderStock);
    }

    @Override
    public OrderStock detail(OrderStockParam orderStockParam) {
        return this.queryOrderStock(orderStockParam);
    }



    /**
     * 获取入库(验车操作记录)
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderStock queryOrderStock(OrderStockParam orderStockParam) {
        OrderStock orderStock = this.getById(orderStockParam.getId());
        if (ObjectUtil.isEmpty(orderStock)) {
            throw new ServiceException(OrderStockExceptionEnum.NOT_EXISTED);
        }
        return orderStock;
    }
}